﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.Controls
{
  public class UserControlExtended : UserControl
  {
    private long _lastMouseDownTicks;

    public event EventHandler Click;
    public event MouseEventHandler DoubleClick;

    public UserControlExtended()
    {
      Loaded += new RoutedEventHandler(ControlLoaded);
    }

    private void ControlLoaded(object sender, RoutedEventArgs e)
    {
      Content.MouseEnter += new MouseEventHandler(ContentMouseEnter);
      Content.MouseLeave += new MouseEventHandler(ContentMouseLeave);
      Content.MouseLeftButtonDown += new MouseButtonEventHandler(ContentMouseLeftButtonDown);
      Content.MouseLeftButtonUp += new MouseButtonEventHandler(ContentMouseLeftButtonUp);
    }

    private void ContentMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      OnMouseLeftButtonUp(e);
    }

    private void ContentMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      OnMouseLeftButtonDown(e);
    }

    private void ContentMouseLeave(object sender, MouseEventArgs e)
    {
      OnMouseLeave(e);
    }

    private void ContentMouseEnter(object sender, MouseEventArgs e)
    {
      OnMouseEnter(e);
    }

    protected bool PlayStoryboard(string name)
    {
      Storyboard storyboard = FindName(name) as Storyboard;
      if (storyboard != null)
      {
        storyboard.Begin();
        return true;
      }
      return false;
    }

    protected virtual new void OnMouseEnter(MouseEventArgs e)
    {
      PlayStoryboard("MouseEnterAnimation");
    }

    protected virtual new void OnMouseLeave(MouseEventArgs e)
    {
      PlayStoryboard("MouseLeaveAnimation");
    }

    protected virtual void OnClick(EventArgs e)
    {
      EventHandler click = Click;
      if (click != null)
        click(this, e);
    }

    protected virtual void OnDoubleClick(MouseEventArgs e)
    {
      MouseEventHandler doubleClick = DoubleClick;
      if (doubleClick != null)
        doubleClick(this, e);
    }

    protected virtual void OnMouseLeftButtonDown(MouseEventArgs e)
    {
      long ticks = DateTime.Now.Ticks;
      TimeSpan span = new TimeSpan(ticks - _lastMouseDownTicks);
      double totalMilliseconds = span.TotalMilliseconds;
      _lastMouseDownTicks = ticks;
      if (totalMilliseconds <= 400)
      {
        OnDoubleClick(e);
        return;
      }

      PlayStoryboard("MouseDownAnimation");
    }

    protected virtual void OnMouseLeftButtonUp(MouseEventArgs e)
    {
      PlayStoryboard("MouseUpAnimation");

      OnClick(EventArgs.Empty);
    }

    #region Custom Layout
    protected virtual Size CustomArrangeOverride(Size finalSize)
    {
      return finalSize;
    }

    protected virtual Size CustomMeasureOverride(Size availableSize)
    {
      return availableSize;
    }

    public Size CustomDesiredSize { get; set; }

    //public void CustomUpdateLayout()
    //{
    //}

    public void CustomArrange(Rect finalRect)
    {
      CustomArrangeOverride(finalRect.GetSize());
    }

    public void CustomMeasure(Size availableSize)
    {
      CustomDesiredSize = CustomMeasureOverride(availableSize);
    }
    #endregion
  }
}